package com.optimus.db.dao.impl;

import com.optimus.common.util.ApplicationException;
import com.optimus.common.util.Convertidor;
import com.optimus.db.DBFactory;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.Date;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author victor
 */
public class BaseDAO {
    protected Session session = null;
    private static  Logger logger = LoggerFactory.getLogger(BaseDAO.class);
    public  BaseDAO () {
        try {
            session=DBFactory.instance().currentFactory().getCurrentSession();
        } catch (Exception e) {
            logger.error("No se puede abrir session a base de datos",e);
        }
    }
    public String getString(Object object, String def, Long ticket){
        return Convertidor.getString(object, def, ticket);
    }
    public Date getDate(Object object, Date def, Long ticket){
        return Convertidor.getDate(object, def, ticket);
    }
    public Integer getInteger(Object object, Integer def, Long ticket){
        return Convertidor.getInteger(object, def, ticket);
    }
    public Float getFloat(Object object, Float def, Long ticket){
        return Convertidor.getFloat(object, def, ticket);
    }
    public BigDecimal getBigDecimal(Object object, BigDecimal def, Long ticket){
        return Convertidor.getBigDecimal(object, def, ticket);
    }
    public void checkSession(){
         try {
            if(!session.isOpen()){
              session = DBFactory.instance().currentFactory().getCurrentSession();
            }
        } catch (MalformedURLException ex) {
            logger.info("Error Reabriendo conexion ", ex);
        } catch (Exception ex) {
            logger.info("Error intentando abrir conexion", ex);
        }
    }
    public Session session() throws ApplicationException {
         try {
              return DBFactory.instance().currentFactory().getCurrentSession();
        } catch (Exception ex) {
            throw new ApplicationException(ex);
        }
    }
}
